﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpAlgorithm
{
    class MLMM
    {
        public MLMM()
        {
            int testCase = GetConsole.GetInt();
            while (testCase-- > 0)
            {
                Tree tree = new Tree();
                string mickle = GetConsole.GetString();
                int littleCount = GetConsole.GetInt();
                for (int i = 0; i < littleCount; i++)
                {
                    string little = GetConsole.GetString();
                    tree.AddWord(little);
                }
                bool[] badPoint = new bool[mickle.Length];
                List<int> roolback = new List<int>();
                Node current = tree.Root.GetChild(mickle[0]);
                if (current == null)
                {
                    Console.WriteLine("Not Value");
                    continue;
                }
                int count = 0;
                for (int i = 1; i < mickle.Length; i++)
                {
                    Node child = current.GetChild(mickle[i]);
                    if (child == null)
                    {
                        if (current.Last)
                        {
                            count++;
                            current = tree.Root;
                            badPoint[i + 1] = true;
                        }
                        else if (roolback.Count == 0)
                        {
                            count = 0;
                            break;
                        }
                        else
                        {
                            i = roolback.Last();
                            roolback.RemoveAt(roolback.Count - 1);
                            current = tree.Root;
                        }
                        i--;
                    }
                    else 
                    {
                        if (current.Last) //분기점일 때
                        {
                            roolback.Add(i);
                        }
                        current = child;
                    }
                }
                if (count == 0)
                {
                    Console.WriteLine("Not Value");
                }
                else
                {
                    Console.WriteLine(count + 1);
                }
            }
        }
        class Tree
        {
            private Node root;
            public Node Root { get { return root; } }
            public Tree()
            {
                root = new Node('s', 0, null);
            }
            public void AddWord(string word)
            {
                Node parentNode = root;
                for (int i = 0; i < word.Length; i++)
                {
                    Node childNode = parentNode.GetChild(word[i]);
                    if (childNode == null)
                    {
                        childNode = new Node(word[i], i + 1, parentNode);
                        parentNode.AddChild(childNode);
                    }
                    parentNode = childNode;
                }
                parentNode = root;
                for (int i = word.Length - 1; i >= 0; i--)
                {
                    Node childNode = parentNode.GetChild(word[i]);
                    if (childNode == null)
                    {
                        childNode = new Node(word[i], i + 1, parentNode);
                        parentNode.AddChild(childNode);
                    }
                    parentNode = childNode;
                }
                //마지막 글자 설정
                parentNode.SetLast();
            }
        }

        class Node
        {
            private char value;
            private int depth;
            private Node parent;
            private List<Node> child;
            private bool last = false;
            //프로퍼티
            public char Value { get { return value; } }
            public Node Parent { get { return parent; } }
            public bool Last { get { return last; } }
            public bool HasChild { get { return child.Count > 0; } }
            public Node(char value, int depth, Node parent)
            {
                this.depth = depth;
                this.value = value;
                this.parent = parent;
                child = new List<Node>();
            }
            public Node GetChild(char value)
            {
                return child.Find(p => p.Value == value);
            }
            public bool AddChild(Node node)
            {
                if (!child.Contains(node))
                {
                    child.Add(node);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            public void SetLast()
            {
                last = true;
            }
            public override string ToString()
            {
                return value.ToString();
            }
        }
    }
    

}
